package com.yuba.front.controller.post;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yuba.common.core.domain.R;
import com.yuba.common.core.validate.AddGroup;
import com.yuba.common.core.validate.EditGroup;
import com.yuba.common.excel.utils.ExcelUtil;
import com.yuba.common.idempotent.annotation.RepeatSubmit;
import com.yuba.common.log.annotation.Log;
import com.yuba.common.log.enums.BusinessType;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.mybatis.core.page.TableDataInfo;
import com.yuba.common.web.core.BaseController;
import com.yuba.front.domain.bo.PostsBo;
import com.yuba.front.domain.vo.PostsVo;
import com.yuba.front.service.IPostsService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帖子管理系统接口
 *
 * @author modakai
 * @date 2024-12-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/posts/posts")
public class PostsController extends BaseController {

    private final IPostsService postsService;

    /**
     * 查询帖子列表
     */
    @SaCheckPermission("posts:posts:list")
    @GetMapping("/list")
    public TableDataInfo<PostsVo> list(PostsBo bo, PageQuery pageQuery) {
        return postsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出帖子列表
     */
    @SaCheckPermission("posts:posts:export")
    @Log(title = "帖子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PostsBo bo, HttpServletResponse response) {
        List<PostsVo> list = postsService.queryList(bo);
        ExcelUtil.exportExcel(list, "帖子", PostsVo.class, response);
    }

    /**
     * 获取帖子详细信息
     *
     * @param postId 主键
     */
    @SaCheckPermission("posts:posts:query")
    @GetMapping("/{postId}")
    public R<PostsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long postId) {
        return R.ok(postsService.queryById(postId));
    }

    /**
     * 新增帖子
     */
    @SaCheckPermission("posts:posts:add")
    @Log(title = "帖子", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PostsBo bo) {
        return toAjax(postsService.insertByBo(bo));
    }

    /**
     * 修改帖子
     */
    @SaCheckPermission("posts:posts:edit")
    @Log(title = "帖子", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PostsBo bo) {
        return toAjax(postsService.updateByBo(bo));
    }

    /**
     * 删除帖子
     *
     * @param postIds 主键串
     */
    @SaCheckPermission("posts:posts:remove")
    @Log(title = "帖子", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] postIds) {
        return toAjax(postsService.deleteWithValidByIds(List.of(postIds), true));
    }
}
